package com.dtjd.playcoinmonkey.activities;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.dtjd.playcoinmonkey.R;
import com.dtjd.playcoinmonkey.adapter.NoticeAdapter;
import com.dtjd.playcoinmonkey.bean.HttpParaBean;
import com.dtjd.playcoinmonkey.bean.NoticeBean;
import com.dtjd.playcoinmonkey.databinding.ActivityNoticeBinding;
import com.dtjd.playcoinmonkey.utils.Network;
import com.dtjd.playcoinmonkey.utils.Tools;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ManageNoticeActivity extends AppCompatActivity {
    private NoticeAdapter adp;
    private final List<NoticeBean> data=new ArrayList<>();
    private boolean isWarn;

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        if (item.getItemId()==R.id.menu_manage_notice_add){
            BottomSheetDialog dialog = new BottomSheetDialog(this);
            @SuppressLint("InflateParams") View commentView = LayoutInflater.from(this).inflate(R.layout.dialog_reply,null);
            final EditText text = commentView.findViewById(R.id.dialog_comment_et);
            final Button bt_comment = commentView.findViewById(R.id.dialog_comment_bt);
            bt_comment.setText(getResources().getString(R.string.submit));
            dialog.setContentView(commentView);
            dialog.show();
            bt_comment.setOnClickListener(view -> {
                dialog.dismiss();
                if (!text.getText().toString().equals("")){
                    List<HttpParaBean> para=new ArrayList<>();
                    para.add(new HttpParaBean("content",text.getText().toString()));
                    para.add(new HttpParaBean("token","token"));
                    Network.post(para, "admin/"+((isWarn)?"addWarn":"addNotice"), new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {e.printStackTrace();}
                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) {
                            runOnUiThread(() -> {
                                try {
                                    Tools.toast( new JSONObject(Objects.requireNonNull(response.body()).string()).getString("datalist"));
                                    refresh(isWarn);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            });
                        }
                    });
                }else {
                    Tools.toast( getResources().getString(R.string.content_cant_blank));
                }
            });
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.manage_notice, menu);
        return true;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityNoticeBinding binding = ActivityNoticeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (getIntent().getStringExtra("type").equals("notice")){
            isWarn=false;
            setTitle(String.format(getResources().getString(R.string.manageNotice),getResources().getString(R.string.notice)));
        }else {
            isWarn=true;
            setTitle(String.format(getResources().getString(R.string.manageNotice),getResources().getString(R.string.warn)));
        }
        ActionBar actionbar = getSupportActionBar();
        if (actionbar!=null){
            actionbar.setDisplayHomeAsUpEnabled(true);
            actionbar.setHomeButtonEnabled(true);
        }
        adp=new NoticeAdapter(this,data);
        binding.noticeList.setAdapter(adp);
        binding.noticeList.setOnItemClickListener((adapterView, view, i, l) -> {
            NoticeBean bean = data.get(i);
            Tools.showDialog(ManageNoticeActivity.this, getResources().getString(R.string.tips), getResources().getString(R.string.sureDelete), getResources().getString(R.string.confirm), (o, dialog) -> {
                dialog.dismiss();
                List<HttpParaBean> para=new ArrayList<>();
                para.add(new HttpParaBean("id",bean.id));
                para.add(new HttpParaBean("token","token"));
                Network.post(para, "admin/"+(isWarn?"deleteWarn":"deleteNotice"), new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                Tools.toast( new JSONObject(Objects.requireNonNull(response.body()).string()).getString("datalist"));
                                refresh(isWarn);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        });
                    }
                });
            }, getResources().getString(R.string.cancel), (o, dialog) -> dialog.dismiss());
        });
        refresh(isWarn);
    }
    private void refresh(boolean isWarn) {
        data.clear();
        List<HttpParaBean> para=new ArrayList<>();
        para.add(new HttpParaBean("getOne","false"));
        Network.post(para, "wbhMain/"+(isWarn?"getWarn":"getNotice"), new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        JSONArray ja = new JSONObject(response.body().string()).getJSONArray("datalist");
                        for (int i = 0; i < ja.length(); i++) {
                            JSONObject jo = ja.getJSONObject(i);
                            data.add(new NoticeBean(true,jo.getString("id"),jo.getString("time"),jo.getString("content")));
                        }
                        adp.notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            }
        });
    }

}